﻿using System;
using System.Threading;
using System.Collections.Concurrent;
using System.Collections.Generic;

namespace SGH
{

    class SGH
    {
        static object workerLocker = new object();
        static int runningWorkers = 100;
        static Random ran = new Random();
       public static int maxSheet = 10000;
       public static int patternNum = 0;
       public static ConcurrentQueue<double[]> queue;

       public SGH()
       {
           queue = new ConcurrentQueue<double[]>();
           int deltaMin;
           int deltaMax;
           double betaMin;
           double betaMax;

           int n = Constant.blankTypeNum;
           
           double miu = Constant.squer / Constant.sheetLength / Constant.sheetWidth;
           //确定参数
           deltaMin = n < 3 ? n : 3;
           deltaMax = n < 15 ? n : 15;
           betaMin = miu < 2 ? miu : 2;
           betaMax = miu < 6 ? miu : 6;
           
           int lamadaMax = (int)(50/ (deltaMax - deltaMin + 1) / (betaMax - betaMin + 1) + 0.999);
           //根据循环次数控制步长
           double betaStep = (betaMax - betaMin) / (deltaMax - deltaMin) / lamadaMax;
           for (int j = deltaMin; j <= deltaMax; j++)
           {
               for (double s = betaMin; s <= betaMax; s += betaStep)
               {
                   double[] a = new double[2];
                   a[0] = j;
                   a[1] = s;
                   queue.Enqueue(a);
               }
           }
       }
        public static void solve()
        {
            //
            runningWorkers = queue.Count;
            while (queue.Count>0)
            {
                double[] d = new double[2];
                queue.TryDequeue(out d);
                //设置线程个数
                ThreadPool.SetMaxThreads(8,8);
                ThreadPool.QueueUserWorkItem(Go, d);
            }
            Console.WriteLine("Waiting for threads to complete...");
            lock (workerLocker)
            {
                while (runningWorkers > 0) Monitor.Wait(workerLocker);
            }
            Console.WriteLine("max="+ maxSheet+"    "+patternNum);
            Console.WriteLine("Complete!");
            Console.ReadLine();
        }

        public static void Go(object instance)
        {
            double[] d = (double[])instance;
            Console.WriteLine("Started: " + d[0]+"   "+d[1]);
            
            Gen gen = new Gen((int)d[0], d[1]);
            gen.gen();
            List<List<int[]>> pattern = gen.pattern;
            double rate=Constant.squer/Constant.sheetLength/Constant.sheetWidth/gen.sheetNum;
            Console.WriteLine("Started: " + d[0] + "   " + d[1] + ":sheetNum" + gen.sheetNum+"  patternNum"+gen.patternNum+"  rate "+rate);
            if (gen.sheetNum < maxSheet)
            {
                maxSheet = gen.sheetNum;
                patternNum = gen.patternNum;
            }
            Console.WriteLine("Ended: " + +d[0] + "   " + d[1]);
            lock (workerLocker)
            {
                 //Test.print(pattern);
                runningWorkers--; Monitor.Pulse(workerLocker);
            }
        }
        
        
    }
}
